package com.lzx.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lzx.article.entity.OneSort;
import com.lzx.article.entity.TwoSort;
import com.lzx.article.mapper.OneSortMapper;
import com.lzx.article.mapper.TwoSortMapper;
import com.lzx.article.service.OneSortService;
import com.lzx.common.utils.R;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lzx
 * @since 2023-01-11
 */
@Service
public class OneSortServiceImpl extends ServiceImpl<OneSortMapper, OneSort> implements OneSortService {
    @Resource
    private OneSortMapper oneSortMapper;

    @Resource
    private TwoSortMapper twoSortMapper;

    //获取一级标签列表
    @Override
    public R getOneSortList() {
        List<OneSort> list = oneSortMapper.selectList(null);
        return R.ok().data("list", list);
    }

    //获取一级和二级标签
    @Override
    public R getOneAndTwoSortList() {
        List<OneSort> oneSortList = oneSortMapper.selectList(null);
        List<OneSort> list = new ArrayList<>();
        for (OneSort oneSort : oneSortList) {
            QueryWrapper<TwoSort> wrapper = new QueryWrapper<>();
            wrapper.eq("parent_id", oneSort.getId());
            List<TwoSort> twoSorts = twoSortMapper.selectList(wrapper);
            if (!twoSorts.isEmpty()) {
                oneSort.setTwoSorts(twoSorts);
            }
            list.add(oneSort);
        }
        return R.ok().data("list", list);
    }
}
